package Class004;

import Utils.ArrayUtils;
import java.util.Arrays;

//基本排序
//注：有序☞从左到右为从小到大
//排序三傻：插入，冒泡，选择
//插入：前面已经有序，后面的数在前面找到它的位置
//冒泡：傻子都会
//选择：前面已经有序，后面找到最小值，放到有序后的第一个位置
public class BasicSorts {
    public static void main(String[] args) {
        test();
    }

    public static void insertionSort(int[] arr) {
        if (arr == null || arr.length < 2) return;
        for (int i = 0; i < arr.length; i++) {
            for (int pre = i - 1; pre >= 0 && arr[pre] > arr[pre + 1]; pre--) {
                ArrayUtils.swap(arr, pre, pre + 1);
            }
//            int curI = i;
//            while(curI > 0) {
//                if(arr[curI] < arr[curI - 1]) {
//                    swap(arr, curI, curI - 1);
//                    curI--;
//                }else break;
//            }
        }
    }

    public static void bubbleSort(int[] arr) {
        if (arr == null || arr.length < 2) return;
        for (int end = arr.length - 1; end >= 0; end--) {
            boolean ordered = true;
            for (int j = 0; j < end; j++) {
                if (arr[j] > arr[j + 1]) {
                    ordered = false;
                    ArrayUtils.swap(arr, j, j + 1);
                }
            }
            if (ordered) {
                break;
            }
        }
    }

    public static void bubbleSortOneLoop(int[] arr) {
        if(arr == null || arr.length < 2) return;
        int N = arr.length;
        int end = N - 2;
        int cur = 0;
        while(end > 0) {
            if(arr[cur] > arr[cur + 1]) {
                ArrayUtils.swap(arr, cur, cur + 1);
            }
            if(cur < end) {
                cur++;
            }else {
                cur = 0;
                end--;
            }
        }
    }

    public static void selectionSort(int[] arr) {
        if (arr == null || arr.length < 2) return;
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            ArrayUtils.swap(arr, i, minIndex);
        }
    }

    public static void mySort(int[] arr) {
//        insertionSort(arr);
        bubbleSortOneLoop(arr);
//        selectionSort(arr);
//        bubbleSort(arr);
    }

    public static void test() {
        int testTime = 10000;
        int err = 0;
        for (int curRound = 1; curRound <= testTime; curRound++) {
            int amount = 1000;
            int max = 100;
            int[] arr = ArrayUtils.generateArray((int) (Math.random() * amount), max);
            int[] stdArr = Arrays.copyOf(arr, arr.length);
            int[] recArrIfErr = Arrays.copyOf(arr, arr.length);
            mySort(arr);
            Arrays.sort(stdArr);
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] != stdArr[i]) {
                    err++;
                    System.out.println("fuck! Cur round:" + curRound + ", cur array:");
                    System.out.println(Arrays.toString(recArrIfErr));
                    System.out.println("Standard ans:");
                    System.out.println(Arrays.toString(stdArr));
                    System.out.println("My ans:");
                    System.out.println(Arrays.toString(arr));
                    System.out.println("=========================");
                    break;
                }
            }
        }
        System.out.println("Overall successful rate:" + 1.0 * (testTime - err) / testTime);
        System.out.println("Test time:" + testTime);
        System.out.println("Total error:" + err);
    }
}
